package sz.szsmk.citizencard.ui.fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.BusHomeSiteCollViewBinding;
import sz.szsmk.citizencard.databinding.FragmentCardlistTicketBinding;
import sz.szsmk.citizencard.ioc.Compontent.DaggerCommonFragmentComponent;
import sz.szsmk.citizencard.model.network.bean.response.QueryInvoiceOpensRes;
import sz.szsmk.citizencard.model.network.bean.response.QueryNearStandbyRes;
import sz.szsmk.citizencard.mvp.contract.IBusSiteCollectContact;
import sz.szsmk.citizencard.mvp.contract.ICardListTicketsContact;
import sz.szsmk.citizencard.mvp.presenter.BusSiteCollectPresenter;
import sz.szsmk.citizencard.mvp.presenter.CardsListTicketsPresenter;
import sz.szsmk.citizencard.ui.adapter.BusSiteNearbyAdapter;
import sz.szsmk.citizencard.ui.adapter.Card_Ticket_Adapter;
import sz.szsmk.citizencard.ui.base.IBaseFragment;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/11/15 20:27
 * description:
 */
public class CardListTicketsFragment extends IBaseFragment<CardsListTicketsPresenter, FragmentCardlistTicketBinding>
implements ICardListTicketsContact.ICardListTicketsView {

    Card_Ticket_Adapter card_ticket_adapter;
    private int currentPage;
    private String pageSize="20";
    public static CardListTicketsFragment getInstance() {
        CardListTicketsFragment sf = new CardListTicketsFragment();
        return sf;
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_cardlist_ticket;
    }

    @Override
    protected void initInjector() {

        DaggerCommonFragmentComponent.builder().appCompontent(DaggerApplication.getAppComponent())
                .build().inject(this);
    }

    @Override
    protected void initViews() {
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getActivity());
        card_ticket_adapter=new Card_Ticket_Adapter();
        dataBinding.cardTicketRv.setLayoutManager(linearLayoutManager);

        dataBinding.cardTicketRv.setLayoutManager(new LinearLayoutManager(getContext()));
        dataBinding.cardTicketRv.setAdapter(card_ticket_adapter);
        dataBinding.swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                currentPage=1;
                mPresenter.getDataList(currentPage+"",pageSize);
            }
        });
        dataBinding.swipeRefreshLayout.autoRefresh();
        View empty = LayoutInflater.from(getContext()).inflate(R.layout.layout_signin_emptydata,null,false);
        ((TextView)empty.findViewById(R.id.tv_tip)).setText("你还没有发票哦~");
        this.card_ticket_adapter.setEmptyView(empty);
    }

    @Override
    protected void updateViews() {

    }

    @Override
    public void getDataListSucc(QueryInvoiceOpensRes response) {
        //替换数据
        if (currentPage == 1){
            this.card_ticket_adapter.replaceData(response.getDetails());
        }else{
            this.card_ticket_adapter.addData(response.getDetails());
        }
        //判断你是否有分页
        boolean hasMore=response.getDetails().size()==20;
        if(hasMore){
            currentPage++;
        }else{
            //完成加载并标记没有更多数据
            dataBinding.swipeRefreshLayout.finishLoadMoreWithNoMoreData();
        }

    }

    @Override
    public void getDataListFail() {
        Log.d("clark","getdetail fail");
    }


    @Override
    public void onResume() {
        super.onResume();
    }
}
